﻿using System;
using System.Collections.Generic;
using System.Xml;

using MonPocGarage.ArmyContracts.Common;
using MonPocGarage.ArmyContracts.Common.Abilities;
using MonPocGarage.ArmyContracts.Common.Enums;
using MonPocGarage.Common.Extensions;
using Action = MonPocGarage.ArmyContracts.Common.Abilities.Action;

namespace MonPocGarage.ArmyData.Builders
{
    internal static class BuildAbilityList
    {
        private readonly static List<IAbility> m_AbilityList = new List<IAbility>();

        internal static List<IAbility> GetAbilityList()
        {
            if (m_AbilityList == null || m_AbilityList.Count == 0)
            {
                m_AbilityList.AddRange(BuildList());
            }
            return m_AbilityList;
        }

        private static List<IAbility> BuildList()
        {
            List<IAbility> abilityList = new List<IAbility>();
            XmlDocument xDoc = new XmlDocument();
            //Load Actions
            xDoc.Load(Environment.CurrentDirectory + "/Data/Abilities/Actions.xml");
            foreach (XmlNode xNode in xDoc.SelectNodes("Actions/Action"))
            {
                Action mappedSkill = new Action(xNode.SelectSingleNode("Name").InnerText,
                                                xNode.SelectSingleNode("Description").InnerText,
                                                AbilityColor.Black);
                abilityList.Add(mappedSkill);
            }
            //Load Advantages
            xDoc.Load(Environment.CurrentDirectory + "/Data/Abilities/Advantages.xml");
            foreach (XmlNode xNode in xDoc.SelectNodes("Advantages/Advantage"))
            {
                Advantage mappedSkill = new Advantage(xNode.SelectSingleNode("Name").InnerText,
                                                      xNode.SelectSingleNode("Description").InnerText,
                                                      AbilityColor.Black);
                abilityList.Add(mappedSkill);
            }
            //Load Reactions
            xDoc.Load(Environment.CurrentDirectory + "/Data/Abilities/Reactions.xml");
            foreach (XmlNode xNode in xDoc.SelectNodes("Reactions/Reaction"))
            {
                Reaction mappedSkill = new Reaction(xNode.SelectSingleNode("Name").InnerText,
                                              xNode.SelectSingleNode("Description").InnerText,
                                              AbilityColor.Black,
                                              xNode.SelectSingleNode("ReactionType").InnerText.ToEnumValue<ReactionType>());
                abilityList.Add(mappedSkill);
            }
            //Load Skills
            xDoc.Load(Environment.CurrentDirectory + "/Data/Abilities/Skills.xml");
            foreach (XmlNode xNode in xDoc.SelectNodes("Skills/Skill"))
            {
                Skill mappedSkill = new Skill(xNode.SelectSingleNode("Name").InnerText,
                                              xNode.SelectSingleNode("Description").InnerText,
                                              AbilityColor.Black,
                                              xNode.SelectSingleNode("SkillType").InnerText.ToEnumValue<SkillType>());
                abilityList.Add(mappedSkill);
            }
            //Load Triggers
            xDoc.Load(Environment.CurrentDirectory + "/Data/Abilities/Triggers.xml");
            foreach (XmlNode xNode in xDoc.SelectNodes("Triggers/Trigger"))
            {
                Trigger mappedSkill = new Trigger(xNode.SelectSingleNode("Name").InnerText,
                                                  xNode.SelectSingleNode("Description").InnerText,
                                                  AbilityColor.Black,
                                                  xNode.SelectSingleNode("TriggerType").InnerText.ToEnumValue<TriggerType>());
                abilityList.Add(mappedSkill);
            }
            return abilityList;
        }
    }
}
